package com.cneport.das.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GavinZhang
 * @version 1.0
 * @desc： 将报文记入文件中
 * @company 东方物通科技有限（北京）公司
 * @date 2021/9/2 14:25
 */
@Component
@Slf4j
public class BizRecorder {


    @Value("${logging.file.path}")
    private String baseDir;

    public void write(String bizType, String idFlag, String content) {
        write(null, bizType, idFlag, content);
    }

    public void write(String senderId, String bizType, String idFlag, String content) {
        if (!StringUtil.isBlank(content)) {
            Date date = new Date();
            String strdate = DateUtil.format(date, "yyyy-MM-dd HHmmssSSS");
            String[] dt = strdate.split(" ");
            String filepath = null;
            if (StringUtil.isBlank(senderId)) {
                filepath = baseDir + File.separator + "biz" + File.separator + dt[0] + File.separator;

            } else {
                filepath = baseDir + File.separator + "biz" + File.separator + senderId + File.separator + dt[0] + File.separator;

            }
            String randStr = RandomGenerator.getNumSequence(4);
            String fileName = bizType + "-" + idFlag + "-" + dt[1] + "-" + randStr + ".log";
            String file = filepath + fileName;
            FileUtil.writeToFile(content, file, "UTF-8");
        }
    }

    private void receive(String baseDir, String bizType, String guid, String fileExt, List<String> contents) {
        if (contents == null || contents.size() == 0) {
            log.info("内容为空，无需写入！");
            return;
        }
        if (StringUtil.isBlank(baseDir)) {
            log.info("baseDir 为空！");
            return;
        }
        int count = 0;
        for (String msg : contents) {
            String fileName = baseDir + File.separator + DateUtil.C10() + File.separator + guid + File.separator + (++count) + ".xml";
            if (!StringUtil.isBlank(bizType)) {
                fileName = baseDir + File.separator + DateUtil.C10() + File.separator + bizType + File.separator + guid + File.separator + (++count) + "." + fileExt;
            }
            msg = new String(Base64Utils.decodeFromUrlSafeString(msg), StandardCharsets.UTF_8);
            FileUtil.writeToFile(msg, fileName, StandardCharsets.UTF_8.toString());
        }
    }

    public void receiveXml(String baseDir, String bizType, String guid, String fileExt, List<String> contents) {
        receive(baseDir, bizType, guid, "xml", contents);
    }

    public void receiveXml(String baseDir, String bizType, String guid, String fileExt, String content) {
        receive(baseDir, bizType, guid, "xml", Arrays.asList(content));
    }

    public void receiveJson(String baseDir, String bizType, String guid, String fileExt, List<String> contents) {
        receive(baseDir, bizType, guid, "json", contents);
    }
}
